var ajax = require('../../utils/ajax.js');
var wxUtil = require('../../utils/wxUtil.js');
var util = require('../../utils/util.js');
var shopInfo = require('../../components/shopInfo/index.js');
var app = getApp();

Page({
  data: {
    orderType: '',
    orderInfo: {},
    logisticsDetails: [],
    shop: null,
  },

  onLoad: function(options) {
    var $this = this;
    var {
      orderId,
      orderType,
      deliveryId,
      expressNumber
    } = options;
    var shop = app.getShop();


    if (deliveryId == 80 && orderType == 3) { //商家自送
      $this.setData({
        orderType,
        deliveryId,
        logisticsDetails: [{
          context: '商家自送，请留意商家电话。',
          onOff: true,
        }],
        shop,
      });
      return;
    }

    ajax.G('/openapi/CHome/orderExpressInfo', {
      orderId,
      expressNumber
    }, function(res) {
      var {
        logisticsName,
        logisticsNumber,
        logisticsDetails,
        dada,
      } = res.result;
      var dadaInfo = {},
        des = '';

      logisticsDetails && (logisticsDetails[0].onOff = true);

      if (dada) {
        if (dada.order_state <= 2) {
          des = '达达配送员取货中，请耐心等待。'
        } else if (dada.order_state == 3) {
          des = '达达配送员送货途中，请留意短信及配送员电话。'
        } else if (dada.order_state == 4) {
          des = '订单已送达。';
        } else if (dada.order_state == 5) {
          des = '达达配送已取消，如有疑问请直接与商家联系。';
        } else if (dada.order_state == 7) {
          des = '达达配送已过期，如有疑问请直接与商家联系。';
        } else {
          des = '';
        }

        logisticsDetails = [{
          context: des,
          time: dada.modify_time,
          onOff: true
        }];

        dadaInfo = {
          name: dada.dm_name,
          phone: dada.dm_mobile,
        };
      }

      $this.setData({
        orderType,
        deliveryId,
        orderInfo: {
          logisticsName,
          logisticsNumber
        },
        logisticsDetails,
        dadaInfo,
        shop,
      });
    });
  },

  /**
   * 调用电话接口
   */
  _callPhone: function(evt) {
    var {
      phone
    } = evt.currentTarget.dataset;

    wx.makePhoneCall({
      phoneNumber: phone
    })
  },

});